% % function ETDRS_drawThrImp(va_pre_t,va_post_t,va_pre_ct,va_post_ct,va_pre_c,va_post_c,cd_pre_t,cd_post_t,cd_pre_ct,cd_post_ct,cd_pre_c,cd_post_c,far);

colors = [2 158 154; 255 189 7; 211 57 33]/255;

va_t = [va_pre_t(:,4) va_post_t(:,4)];
va_ct = [va_pre_ct(:,4) va_post_ct(:,4)];
va_c = [va_pre_c(:,4) va_post_c(:,4)];

cd_t = (-[cd_pre_t(:,4) cd_post_t(:,4)] +445)/10;
cd_ct = (-[cd_pre_ct(:,4) cd_post_ct(:,4)] +445)/10;
cd_c = (-[cd_pre_c(:,4) cd_post_c(:,4)] +445)/10;

% % imp_va_t = mean(exp(va_pre_t(:,1:3)) - exp(va_post_t(:,1:3)),2)./mean(exp(va_pre_t(:,1:3)),2); % Not significant p = 0.0560
% % imp_va_ct = mean(exp(va_pre_ct(:,1:3)) - exp(va_post_ct(:,1:3)),2)./mean(exp(va_pre_ct(:,1:3)),2); % Significant improvement p = 0.0113
% % imp_va_c = mean(exp(va_pre_c(:,1:3)) - exp(va_post_c(:,1:3)),2)./mean(exp(va_pre_c(:,1:3)),2); % Not significant
% imp_va_t = (exp(va_t(:,1)) - exp(va_t(:,2)))./exp(va_t(:,1));
% imp_va_ct = (exp(va_ct(:,1)) - exp(va_ct(:,2)))./exp(va_ct(:,1));
% imp_va_c = (exp(va_c(:,1)) - exp(va_c(:,2)))./exp(va_c(:,1));
imp_va_t = (mean(exp(va_pre_t(:,1:3)),2)-mean(exp(va_post_t(:,1:3)),2))./mean(exp(va_pre_t(:,1:3)),2);
imp_va_ct = (mean(exp(va_pre_ct(:,1:3)),2)-mean(exp(va_post_ct(:,1:3)),2))./mean(exp(va_pre_ct(:,1:3)),2);
imp_va_c = (mean(exp(va_pre_c(:,1:3)),2)-mean(exp(va_post_c(:,1:3)),2))./mean(exp(va_pre_c(:,1:3)),2);



% % % imp_cd_t = mean(((445-cd_pre_t(:,1:3))/10) - (445-cd_post_t(:,1:3))/10,2)./mean((445-cd_pre_t(:,1:3))/10,2); % Not significant p = 0.2747
% % % imp_cd_ct = mean(((445-cd_pre_ct(:,1:3))/10) - (445-cd_post_ct(:,1:3))/10,2)./mean((445-cd_pre_ct(:,1:3))/10,2); % Significant improvement p = 0.0122
% % % imp_cd_c = mean(((445-cd_pre_c(:,1:3))/10) - (445-cd_post_c(:,1:3))/10,2)./mean((445-cd_pre_c(:,1:3))/10,2); % Not significant
% imp_cd_t = (cd_t(:,1) - cd_t(:,2))./cd_t(:,1);
% imp_cd_ct = (cd_ct(:,1) - cd_ct(:,2))./cd_ct(:,2);
% imp_cd_c = (cd_c(:,1) - cd_c(:,2))./cd_c(:,1);
imp_cd_t = (mean(445 - cd_pre_t(:,1:3),2)-mean(445- cd_post_t(:,1:3),2))./mean(445-cd_pre_t(:,1:3),2);
imp_cd_ct = (mean(445 - cd_pre_ct(:,1:3),2)-mean(445- cd_post_ct(:,1:3),2))./mean(445-cd_pre_ct(:,1:3),2);
imp_cd_c = (mean(445 - cd_pre_c(:,1:3),2)-mean(445- cd_post_c(:,1:3),2))./mean(445-cd_pre_c(:,1:3),2);

% 
% figure('color',[1 1 1])
% plot(imp_cd_t,imp_va_t,'ko','MarkerFaceColor',colors(1,:)); hold on;
% plot(imp_cd_ct,imp_va_ct,'ko','MarkerFaceColor',colors(2,:)); hold on;
% plot(imp_cd_c,imp_va_c,'ko','MarkerFaceColor',colors(3,:)); hold on;
% legend('Alternating','Fixed','Control','location','best');
% xlabel('Confortable Distance Decrease (%)');
% ylabel('Visual Acuity Improvement (%)');

alpha = 0.05;


disp(' ' )
disp(' ' )
disp(' ' )
disp('Alternating Versus Fixed')
disp('Alternating Versus Fixed')
disp('Alternating Versus Fixed')
% compare training versus control with training
input1 = [repelem([1 2]',8) [imp_cd_t imp_va_t; imp_cd_ct imp_va_ct]];
HotellingT2_mine(input1,alpha)


disp(' ' )
disp(' ' )
disp(' ' )
disp('Alternating Versus Control')
disp('Alternating Versus Control')
disp('Alternating Versus Control')
% compare training versus control without training
input2 = [repelem([1 2]',8) [imp_cd_t imp_va_t; imp_cd_c imp_va_c]];
HotellingT2_mine(input2,alpha)

disp(' ' )
disp(' ' )
disp(' ' )
disp('Fixed Versus Control')
disp('Fixed Versus Control')
disp('Fixed Versus Control')
% compare control with training versus contol without training
input3 = [repelem([1 2]',8) [imp_cd_ct imp_va_ct; imp_cd_c imp_va_c]];
HotellingT2_mine(input3,alpha)

%%
xs_t = [mean(imp_cd_t) mean(imp_cd_ct) mean(imp_cd_c)]*100;
ys_t = [mean(imp_va_t) mean(imp_va_ct) mean(imp_va_c)]*100;
xs_e = 100*[std(imp_cd_t) std(imp_cd_ct) std(imp_cd_c)]/sqrt(8);
ys_e = 100*[std(imp_va_t) std(imp_va_ct) std(imp_va_c)]/sqrt(8);

colors2 = colors*1.2;
colors2(colors2>1) = 1;

figure('color',[1 1 1])
% subplot(1,1,1)
subplot(1,2,1)
yline(0,'--k'); hold on; xline(0,'--k'); hold on;

% without marker edge
% % plot(100*imp_cd_t,100*imp_va_t,'o','MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colors2(1,:),'MarkerSize',7); hold on
% % plot(100*imp_cd_ct,100*imp_va_ct,'o','MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colors2(2,:),'MarkerSize',7); hold on
% % plot(100*imp_cd_c,100*imp_va_c,'o','MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colors2(3,:),'MarkerSize',7); hold on

% with marker edge
% plot(100*imp_cd_t,100*imp_va_t,'o','MarkerEdgeColor' ,[0 0 0], 'MarkerFaceColor' ,colors2(1,:),'MarkerSize',7); hold on
% plot(100*imp_cd_ct,100*imp_va_ct,'o','MarkerEdgeColor' ,[0 0 0], 'MarkerFaceColor' ,colors2(2,:),'MarkerSize',7); hold on
% plot(100*imp_cd_c,100*imp_va_c,'o','MarkerEdgeColor' ,[0 0 0], 'MarkerFaceColor' ,colors2(3,:),'MarkerSize',7); hold on

P1 = plot(100*imp_cd_t,100*imp_va_t,'o','MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerFaceColor' ,colors2(1,:),'MarkerSize',7); hold on
%  legend('Alternating')
P2 = plot(100*imp_cd_ct,100*imp_va_ct,'o','MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerFaceColor' ,colors2(2,:),'MarkerSize',7); hold on
%  legend('Alternating','Fixed')
P3 = plot(100*imp_cd_c,100*imp_va_c,'o','MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerFaceColor' ,colors2(3,:),'MarkerSize',7); hold on

l = cell(1,3);
l{1}='Alternating'; l{2}='Fixed'; l{3}='Control';
[hLg, icons] = legend([P1,P2, P3],l,'location','northwest');
legend boxoff


% legend('Alternating','Fixed', 'control')
% 
% legend boxoff
% legend('Pre','Post','o','Location','best')
% 
%remove outlier
% plot(100*imp_cd_ct(3),100*imp_va_ct(3),'x','color',colors2(2,:)); hold on
% plot(100*imp_cd_c(3),100*imp_va_c(3),'x','color',colors2(3,:)); hold on
% % plot(100*imp_cd_ct(3),100*imp_va_ct(3),'kx','MarkerFaceColor',[1 1 1], 'MarkerSize',15, 'linewidth',1.5); hold on
% % plot(100*imp_cd_c(3),100*imp_va_c(3),'kx','MarkerFaceColor',[1 1 1], 'MarkerSize',15, 'linewidth',1.5); hold on
% final (to mark outlier)
plot(100*imp_cd_ct(3),100*imp_va_ct(3),'kx','MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerSize',15, 'linewidth',1.5); hold on
plot(100*imp_cd_c(3),100*imp_va_c(3),'kx','MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerSize',15, 'linewidth',1.5); hold on


e1 = errorbarxy(xs_t(1),ys_t(1),xs_e(1),ys_e(1),{'ko','k','k','[0.0078 0.6196 0.6039]'}); hold on
e2 = errorbarxy(xs_t(2),ys_t(2),xs_e(2),ys_e(2),{'ko','k','k','[1.0000 0.7412 0.0275]'}); hold on
e3 = errorbarxy(xs_t(3),ys_t(3),xs_e(3),ys_e(3),{'ko','k','k','[0.8275 0.2235 0.1294]'}); hold on

% legend([p1 p2 p3],'Alternating','Fixed','Control','location','best');
box off
xlabel('Adjusted near distance-to-eye (%)', 'FontName', 'Arial');
ylabel('VA improvement (%)', 'FontName', 'Arial');
% % set(gca,'FontSize', 10, 'linewidth',1);
set(gca,'xlim',[-25 35],'ylim',[-15 25], 'FontName', 'Arial', 'FontSize', 10, 'linewidth',1);


title('Near distance', 'FontName', 'Arial');
 set(gcf,'units','centimeters','position',[0 0 19 9])





imp_t_f = 100*(exp(far.t_pre) - exp(far.t_post))./exp(far.t_pre);
imp_ct_f = 100*(exp(far.ct_pre) - exp(far.ct_post))./exp(far.ct_pre);
imp_c_f = 100*(exp(far.c_pre) - exp(far.c_post))./exp(far.c_pre);

% % imp_t_n = 100*(imp_va_t + imp_cd_t)/2;
% % imp_ct_n = 100*(imp_va_ct + imp_cd_ct)/2;
% % imp_c_n = 100*(imp_va_c + imp_cd_c)/2;

% % dataF = [imp_t_n imp_t_f imp_ct_n imp_ct_f imp_c_n imp_c_f];

dataF = [imp_t_f imp_ct_f imp_c_f];

% xaxisF = [1 2 4 5 7 8];
xaxisF = [1 4 7];
% colorsF = repelem(colors,2,1);
colorsF = [0.0078  0.6196 0.6039; 1.0000  0.7412 0.0275; 0.8275 0.2235 0.1294];
% colorsF2 = repelem(colors2,3,1);
colorsF2 = [0.0094 0.7435 0.7247; 1.0000 0.8894 0.0329; 0.9929 0.2682 0.1553];

% % figure(2)
% % subplot(1,1,1)
subplot(1,2,2)
% % for bb = 1:6
for bb = 1:3
    b = bar(xaxisF(bb),mean(dataF(:,bb)));
    b.FaceColor = 'flat';
    b.LineWidth= 1;
%     b.BarWidth = 1.8; % For the manuscript (main)
% Adjust the barwidth for the supplementary fig.
    b.BarWidth = 0.8;
    b.CData(1,:) = colorsF(bb,:);
    hold on
    % without marker edge
%      plot(xaxisF(bb),dataF(:,bb),'o', 'MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colorsF2(bb,:),'MarkerSize',7);
   % wit marker edge
%     plot(xaxisF(bb),dataF(:,bb),'o', 'MarkerEdgeColor' ,[0 0 0], 'MarkerFaceColor' ,colorsF2(bb,:),'MarkerSize',7);
    plot(xaxisF(bb),dataF(:,bb),'o', 'MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerFaceColor' ,colorsF2(bb,:),'MarkerSize',7);
    hold on
    bars{bb,1} = b;
end


% remove outlier

% plot(xaxisF(3),dataF(3,3),'x','color',colorsF2(3,:),'MarkerSize',15,'linewidth',1.5); hold on
% plot(xaxisF(3),dataF(6,3),'x','color',colorsF2(3,:),'MarkerSize',15,'linewidth',1.5); hold on
% % plot(xaxisF(3),dataF(3,3),'kx','MarkerFaceColor',[1 1 1],'MarkerSize',15,'linewidth',1.5); hold on
% % plot(xaxisF(3),dataF(6,3),'kx','MarkerFaceColor',[1 1 1],'MarkerSize',15,'linewidth',1.5); hold on
% final (to mark outlier)
plot(xaxisF(3),dataF(2,3),'x','MarkerEdgeColor' ,[0.7 0.7 0.7],'MarkerSize',15,'linewidth',1.5); hold on % Found it later (March 9, 2021)
plot(xaxisF(3),dataF(3,3),'x','MarkerEdgeColor' ,[0.7 0.7 0.7],'MarkerSize',15,'linewidth',1.5); hold on
plot(xaxisF(3),dataF(6,3),'x','MarkerEdgeColor' ,[0.7 0.7 0.7],'MarkerSize',15,'linewidth',1.5); hold on


errorbar(xaxisF,mean(dataF),std(dataF)/sqrt(8),'ko','MarkerFaceColor',[1 1 1],'linewidth',1,'CapSize',0)
% % set(gca,'xtick',xaxisF,'xticklabel',{'Near','Far','Near','Far','Near','Far','Near','Far'},'ylim',[-15 30],'fontsize',10,'linewidth',1);

% hold off
% box off
% l = cell(1,3);
% l{1}='Alternating'; l{2}='Fixed'; l{3}='Control';
% [hLg, icons] = legend([bars{1,1},bars{3,1}, bars{5,1}],l,'location','northwest');
% legend boxoff
% hold off
% box off

set(gca,'xtick',xaxisF,'xticklabel',{'Alternating','Fixed','Control'},'ylim',[-5 15], 'FontName', 'Arial','fontsize',10,'linewidth',1);
 box off




% l = cell(1,3);
% l{1}='Alternating'; l{2}='Fixed'; l{3}='Control';
% [hLg, icons] = legend([bars{1,1},bars{3,1}, bars{5,1}],l,'location','northwest');
% legend boxoff

% % icons = findobj(icons,'Type','patch');
% % % icons = findobj(icons,'Marker','none','-xor');
% % set(icons(1),'Vertices',[3*(icons(1).Vertices(:,1)/4) (icons(1).Vertices(:,2))]);
% % set(icons(2),'Vertices',[3*(icons(2).Vertices(:,1)/4) (icons(2).Vertices(:,2))]);
% % set(icons(3),'Vertices',[3*(icons(3).Vertices(:,1)/4) (icons(3).Vertices(:,2))]);

% box off
% set(gca,'xticklabel',{'Alternating','Fixed','Control'});
ylabel('Performance improvement (%)', 'FontName', 'Arial');
title('Far distance', 'FontName', 'Arial');



% ms = [mean([imp_t_f imp_t_n*100],2) mean([imp_ct_f imp_ct_n*100],2) mean([imp_c_f imp_c_n*100],2)];
% mm = mean(ms);
% se = std(ms)/sqrt(8);
% 
% 
% subplot(1,2,2)
% for bb = 1:3
%     b = bar(bb,mm(bb));
%     b.FaceColor = 'flat';
%     b.LineWidth = 1;
%     b.CData(1,:) = colors(bb,:);
%     hold on
% end
% 
% 
% p1 = plot(1,ms(:,1),'o', 'MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colors2(1,:),'MarkerSize',3);
% hold on
% p2 = plot(2,ms(:,2),'o','MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colors2(2,:),'MarkerSize',3);
% hold on
% p3 = plot(3,ms(:,3),'o','MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colors2(3,:),'MarkerSize',3);
% hold on
% 
% errorbar([1 2 3],mm,se,'ko','MarkerFaceColor',[1 1 1],'linewidth',1,'CapSize',0)
% set(gca,'xtick',[],'ylim',[-6 18]);
% l = cell(1,3);
% l{1}='Alternating'; l{2}='Fixed'; l{3}='Control';
% [hLg, icons] = legend(l,'location','northwest');
% legend boxoff
% 
% icons = findobj(icons,'Type','patch');
% % icons = findobj(icons,'Marker','none','-xor');
% set(icons(1),'Vertices',[3*(icons(1).Vertices(:,1)/4) (icons(1).Vertices(:,2))]);
% set(icons(2),'Vertices',[3*(icons(2).Vertices(:,1)/4) (icons(2).Vertices(:,2))]);
% set(icons(3),'Vertices',[3*(icons(3).Vertices(:,1)/4) (icons(3).Vertices(:,2))]);
% 
% box off
% % set(gca,'xticklabel',{'Alternating','Fixed','Control'});
% ylabel('Performance Improvement (%)');
% title('Near + Far Distance');
% 
% ano_imp_far = [];
% ano_imp_far(:,1) = 1:24';
% ano_imp_far(:,2) = repelem((1:3)',8,1);
% ano_imp_far(:,3) = [imp_t_f; imp_ct_f; imp_c_f];
% 
% % participant - group - distance - PI

% % set(gcf,'units','centimeters','position',[0 0 19 9])

set(gcf,'units','centimeters','position',[0 0 24 9])

% csvwrite('ano_imp_far.csv',ano_imp_far)
% 

 % Significance test: (*p < 0.05; **p < 0.01; ***p < 0.001).
 [h,p,ci,stats] = ttest(imp_t_f); % *p = 0.0195,t7 = 3.0170 significant 
 [h,p,ci,stats] = ttest(imp_ct_f); % *p =  0.0063, t7 = 3.8505 
 [h,p,ci,stats] = ttest(imp_c_f); % p = 0.7834, t7 = 0.2856 
 